package com.becca.cake.controller.advert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.advert.AdvertModel;
import com.becca.cake.service.advert.IAdvertService;
import com.becca.cake.util.JsonUtil;
/**
 * 广告查询Controller
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年5月27日 下午4:12:37
 */
@Controller
@RequestMapping("/advertManager")
public class AdvertAction  extends BaseAction {
    private final static Log logger = LogFactory.getLog(AdvertAction.class);
    
    @Autowired
    IAdvertService iAdvertService;
    
    /**
     * 广告查询
     * @param advertModel
     * @return
     */
    @RequestMapping(value = "/selectAdverts", method = RequestMethod.POST)
    @ResponseBody
    public String selectAdverts(AdvertModel advertModel,String formStartDate,String formEndDate){
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("advertModel", advertModel);
        map.put("pagingBean", page);
        map.put("formStartDate", formStartDate);
        map.put("formEndDate", formEndDate);
        try {
            // stmp1:获取广告数据
            List<AdvertModel> list = this.iAdvertService.selectAdverts(map);
            // stmp2:获取广告总条数
            Long count = this.iAdvertService.selectAdvertsCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 添加或修改广告信息
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/insertOrUpdateAdvert", method = RequestMethod.POST)
    @ResponseBody
    public  String insertOrUpdateSubject(HttpServletRequest request, AdvertModel advertModel) {
        //step1:获取图片file
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile files = multipartRequest.getFile("upload");
        try {
            this.iAdvertService.insertOrUpdateAdvert(advertModel, files, getTempFoderPath(),this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("操作广告信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 根据id查询广告对象
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/selectAdvertById", method = RequestMethod.POST)
    @ResponseBody
    public String selectSubjectUpdateById(@RequestParam("advertId") Integer advertId) {
        try {
            //step1:请求参数
            AdvertModel advertModel = this.iAdvertService.selectAdvertById(advertId);
            return JsonUtil.writeObjectFormJSON(advertModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 根据id启用或禁用广告对象
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/updateAdvStatusById", method = RequestMethod.POST)
    @ResponseBody
    public String updateAdvStatusById(@RequestParam(value = "advertId", required = false) Integer advertId,
                                      @RequestParam(value = "status", required = false) String status) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("advertId", advertId);
            paramMap.put("status", status);
            paramMap.put("userName", this.getUserSeesion().getTrueName());
            this.iAdvertService.updateAdvStatusById(paramMap);
            return JsonUtil.returnJsonSuccess("操作广告信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 批量修改Order排序列
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/updateAdvert", method = RequestMethod.POST)
    @ResponseBody
    public String updateAdvert(@RequestParam(value = "jsonList", required = false)String jsonList) {
        try {
            this.iAdvertService.updateOrders(jsonList);
            return JsonUtil.returnJsonSuccess("操作广告信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
}
